package com.cqvie.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cqvie.model.Teacher;

@Repository("teacherDao")
public class TeacherDao {
	@Resource
	private SessionFactory sessionFactory;

	// 根据教师的ID和密码进行教师登录
	public boolean selectByTeacher(int Id, String pwd) {
		String hql = "from Teacher t where t.teacherID=? and t.password=?";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setInteger(0, Id);
		query.setString(1, pwd);
		if (query.list().size() >= 1) {
			return true;
		}
			return false;
	}
	
	// 查根据教师的ID查询教师的ID和Name
		public Teacher selectTeacherByID(int Id) {
			String hql = "from Teacher t where t.teacherID=?";
			Session session = sessionFactory.getCurrentSession();
			Query query = session.createQuery(hql);
			query.setInteger(0, Id);
			Teacher t=(Teacher) query.list().get(0);
			if (query.list().size() >= 1) {
				return t;
			}else
				return null;
		}
	
	//根据教师ID来查询教师
		public List<Teacher> selectByID(int id){
			String hql="from Teacher t where t.teacherID=?";
			Session session = sessionFactory.getCurrentSession();
			Query query = session.createQuery(hql);
			query.setInteger(0, id);
			@SuppressWarnings("unchecked")
			List<Teacher> listTeacher=query.list();
			return listTeacher;
		}
		
	//添加教师信息
	public void addTeacher(Teacher t){
		Session session=sessionFactory.getCurrentSession();
		session.save(t);
	}
	
	
	//添加班主任信息
			public void addTeacherMaster(Teacher mt) {
				Session session=sessionFactory.getCurrentSession();
				mt.setIsTeachMaster((byte) 0);
				session.save(mt);
				}
}
